<?php
/**
 * H5支付.
 */

namespace app\api\service\payment;

use app\api\service\ApiPayment;
use app\common\library\exception\OrderException;
use app\api\service\payment\F4stpay\Client as F4stpayClient;

class F4stpay extends ApiPayment
{
    public function alipay($order)
    {
        return $this->requestApi($order);
    }

    public function weixin($order)
    {
        return $this->requestApi($order, 'wechat');
    }

    public function wechat_h5($order)
    {
        return $this->requestApi($order, 'wechat_h5');
    }

    public function alipay_h5($order)
    {
        return $this->requestApi($order, 'alipay_h5');
    }

    public function ysf($order)
    {
        return $this->requestApi($order, 'ysf');
    }

    public function requestApi($order, $type = 'alipay')
    {
        $client  = new F4stpayClient($this->config['mercId'], $this->config['mercKey']);
        $orderData = array(
            "mercOrderId" => $order['trade_no'],
            "amount" => $order['amount'],
            "channel" => $type,
            "productName" => $order['subject'],
            "notifyUrl" => "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/notify/F4stpay"
            // "notifyUrl" => "http://juyouzhifu.com/notify/F4stpay"
        );

        try {
            $result = $client->createPayOrder($orderData);
            if ($result['data']['qrCode']) {
                return [
                    'order_id' => $order['out_trade_no'],
                    'pay_seq' => $order['trade_no'],
                    'pay_url' => $result['data']['qrCode'],
                    'pay_app_url' => $result['data']['qrCode']
                ];
            }

        } catch (\F4stPay\Exceptions\F4stPayException $e) {
            // print $e->getMessage();
        }

        return false;
    }

    public function notify()
    {
        return $this->verifyOrderNotify();
    }

    /**
     * 回调验签.
     *
     *
     * @return array
     *
     * @throws OrderException
     */
    private function verifyOrderNotify()
    {
        $xml = file_get_contents('php://input');
        file_put_contents("/tmp/F4stpay.log", var_export($_POST, true), FILE_APPEND);
        file_put_contents("/tmp/F4stpay_get.log", var_export($_GET, true), FILE_APPEND);
        file_put_contents("/tmp/F4stpay_input.log", $xml, FILE_APPEND);

        if ($_REQUEST['orderStatus'] == "paid"){
            return array(
                'trade_no' => $_REQUEST['mercOrderId'],
            );
        } 

        return false;

        $xml = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        //Object  对象
        $response = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
        //读订单对应的支付渠道配置
        $this->config = self::getOrderPayConfig($response['out_trade_no']);
        $req = new Request($this->config);
        $result = $req->callback();
        if ($result) {
            return array(
                'trade_no' => $result['mercOrderId'],
            );
        }
        
        return false;
    }

}
